$(function () {
    var swa = $('.dymswitch-a');
    var swb = $('.dymswitch-b');
    var swc = $('.dymswitch-c');
    swa.click(function () {
        var self = $(this);
        var index = swa.index(swa.filter('.cur'));
        var sIndex = swa.index(self);
        if (index != sIndex) {
            $(swa[index]).removeClass('cur');
            self.addClass('cur');
            $(swb[index]).hide();
            $(swb[sIndex]).show();
            if (index == 1) {
                swc.hide();
            }
        }
    });

    $.tools.validator.fn('[data-equals]', function (input) {
        var name = input.attr('data-equals');
        field = this.getInputs().filter('[name=' + name + ']');
        if (input.val() != field.val()) {
            $('#err_' + input.attr('id')).show();
            $('#err_' + input.attr('id')).html('<p style="color:red;">密码与确认密码不一致。</p>');
            return false;
        } else {
            $('#err_' + input.attr('id')).hide();
            return true;
        }
    });

    var forms = $('form');
    var submits = $('.submit');
    forms.validator({'message':false}).submit(function (e) {
        var form = $(this);
        if (!e.isDefaultPrevented()) {
            var fid = form.attr('id');
            if (fid == 'reg-form')
                register(form, e);
            else if (fid == 'login-form')
                login(form, e);
        }
        e.preventDefault();
    });

    submits.click(function () {
        var self = $(this);
        var index = submits.index(self);
        var form = $(forms[index]);
        form.submit();
        return false;
    });
    
    $('#reg-form :email').blur(function () {
        var self = $(this);
        if (self.val() != '') {
            exists_email(self.val());
        }
    });
});
function login(form, e) {
    $('#err_username').hide();
    $('._login_loading').show();
    execute('login_bind', {
        'email': $('#username').val(),
        'pwd': $('#pwd').val(),
        'remeber': $('#login-rem').attr('checked') == 'checked',
        'url': $.query.get('url')
        }
        , function (resp) {
            eval('var r = ' + resp + ';');
            if (r.status == 0) {
                // 登录成功
                if (r.url) {
                    top.location.href = r.url;
                } else {
                    top.location.href = '/';
                }
            } else if (r.status == 2) {
                // 会话超时
                // 回首页
                top.location.href = '/';
            } else if (r.msg != '') {
                // 其他错误状态
                $('#err_username').show();
                $('#err_username').html('<div style="color:red">' + r.msg + '</div>');
                $('._login_loading').hide();
            }
        }
    );
};
function register(form, e) {
    exists_email(form.find(':email').val(), function (response) {
        eval('var r = ' + response + ';');
        if (r.status != 0) {
            $('#email').addClass('invalid');
            $('#err_email').html('<p style="color:red">' + r.msg + '</p>');
        } else {
            $('#email').removeClass('invalid');
            $('#err_email').html('<p>请输入正确的邮箱，如：hi@xunbaotu.com</p>');
            $('._loading').show();
            execute('reg', {
                'email':form.find(':email').val(),
                'pswd':form.find('#pswd').val(),
                'pscwd':form.find('#pscwd').val(),
                'depart':form.find('#depart').val(),
                'url':$.query.get('url')
                }
                , function (resp) {
                    eval('var re = ' + resp + ';');
                    if (re.status == 0) {
                        $($('.dymswitch-b')[0]).remove();
                        $('.dymswitch-c').show();
                        $('#receive_mail').attr('href', re.po);
                    } else if (re.status == 2) {
                        // 会话超时
                        top.location.href = '/';
                    }
                }
            );
        }
    });
};
function exists_email(email, callback) {
    execute('exists_email', {'email': email}, callback || function (resp) {
        eval('var r = ' + resp + ';');
        if (r.status != 0) {
            $('#email').addClass('invalid');
            $('#err_email').html('<p style="color:red">' + r.msg + '</p>');
        } else {
            $('#email').removeClass('invalid');
            $('#err_email').html('<p>请输入正确的邮箱，如：hi@xunbaotu.com</p>');
        }
    });
};
